const baseUrl = 'http://localhost:8000';

function getData(url, data = {}, method = 'get') {
  return new Promise((resolve, reject) => {
    wx.showLoading({
      title: '努力加载中...',
    })
    let header = method === 'post' ? {
      "Content-type": "application/x-www-form-urlencoded"
    } : {}

    wx.request({
      url: baseUrl + url,
      method: method,
      // header: {
      //   'user-token':'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwaG9uZSI6IjEzODExMTEyMjIyIiwidXNlcklkIjoiNjQ2NmMxMTU5ZGRhMDE3YjQyZTAwMGYxIiwiaWF0IjoxNjk4NTkwNzA3LCJleHAiOjE3MDIxOTA3MDd9.dN8J9y5NiBZqjIkNYznY8S5YdhC0R8I1yHgEQxWNYxs'
      // },
      header,
      data,
      success: (res) => {
        if (res.data.code == 1) {
          resolve(res.data);
        } else {
          wx.showToast({
            title: res.data.msg,
          })
        }
      },
      fail: (err) => {
        console.log(err);
      },
      complete: () => {
        wx.hideLoading();
      }
    })
  })
}

export const get = (url, data = {}) => {
  return getData(url, data, 'get')
}
export const post = (url, data = {}) => {
  return getData(url, data, 'post')
}

export default {
  get,
  post
}